<?php

namespace App\Admin\Controllers\Vote\Chapter;

use App\Admin\Actions\GoBackButton;
use App\Admin\Renders\CommonGrid;
use App\Models\Chapter;
use App\Models\Comment;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;

class CommentController extends Controller
{
    public function index(Content $content, Chapter $chapter): Content
    {
        return $content->header('评论管理')
            ->description($chapter->title)
            ->body($this->grid($chapter));
    }

    public function grid(Chapter $chapter): Grid
    {
        $model = Comment::ofAdmin()
            ->where('chapter_id', $chapter->getKey())
            ->with(['user']);

        return Grid::make($model, function (Grid $grid) {
            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->tools([new GoBackButton()]);

            CommonGrid::comment($grid);
        });
    }

    public function destroy(Chapter $chapter, int|string $id): JsonResponse
    {
        return $this->form($chapter)->destroy($id);
    }

    public function form(Chapter $chapter): Form
    {
        return Form::make(Comment::ofAdmin(), function (Form $form) use ($chapter) {
            $form->hidden('chapter_id')
                ->value($chapter->getKey());
        });
    }
}
